package icu.zlz.message.component;


import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import com.rabbitmq.client.Channel;

import java.io.IOException;

import static icu.zlz.common.rabbitmq.configure.RabbitMQConfig.*;


/**
 * 消息队列监听
 */
@Component
public class MqMsg {

    /**
     * 监听队列并处理消息
     *
     * @param message 消息对象
     * @param channel RabbitMQ 通道
     * @throws IOException 异常
     */
    @RabbitListener(queues = PHONE_QUEUE)
    public void receivePhoneMessage(Message message, Channel channel) throws IOException {
        try {
            // 处理消息
            String body = new String(message.getBody());

            //业务逻辑

            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 处理失败，拒绝消息并重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
    /**
     * 监听队列并处理消息
     *
     * @param message 消息对象
     * @param channel RabbitMQ 通道
     * @throws IOException 异常
     */
    @RabbitListener(queues = EMAIL_QUEUE)
    public void receiveEmailMessage(Message message, Channel channel) throws IOException {
        try {
            // 处理消息
            String body = new String(message.getBody());

            //业务逻辑

            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 处理失败，拒绝消息并重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
